package useless.wecker;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Weckzeit extends Thread{

    public int stunde;
    public int minute;
    public Boolean stop;

    private Boolean kaffeeIstAn;
    private Boolean lichtIstAn;
    
    
    
    
    public Weckzeit(int stunde, int minute) {
        this.stunde = stunde;
        this.minute = minute;
        this.stop = false;
        Terminal.SET_NEXT_TIMER(stunde, minute, 0);
        Terminal.SET_KAFFEE_STATUS(false);
        Terminal.SET_LICHT_STATUS(false);
        kaffeeIstAn = false;
        lichtIstAn = false;
        
    }

    @Override
    public void run() {
        while(!UselessWecker.STOP && !stop){
            if (isPassed() && !lichtIstAn){
                Schalter.allesAn();
                kaffeeIstAn = true;
                lichtIstAn = true;
                Terminal.SET_KAFFEE_STATUS(true);
                Terminal.SET_LICHT_STATUS(true);
            }
            else if (isInRange() && !kaffeeIstAn){
                Schalter.kaffeeAn();
                kaffeeIstAn = true;
                Terminal.SET_KAFFEE_STATUS(true);
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

    @Override
    public synchronized void start() {
        super.start(); //To change body of generated methods, choose Tools | Templates.
    }
    
    
    public String getCurrentTimeString() {
        Calendar cal = Calendar.getInstance();
        int hour = cal.get(cal.HOUR_OF_DAY);
        int minute = cal.get(cal.MINUTE);
        
        return hour + ":" + minute + ":00";
    }
    public long timeDiff() {
        try {
            String time1 = this.toString();
            String time2 = getCurrentTimeString(); //"19:00:00";
            
            SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
            Date date1 = format.parse(time1);
            Date date2 = format.parse(time2);
            long difference = date2.getTime() - date1.getTime();
            difference = difference / 1000;
            difference = difference / 60;
            
            
            return difference;
        } catch (ParseException ex) {
            System.out.println("ParseException in TimeDiff Method !!????!! - Das sollte eigentlich nicht passieren.");
            return 500000;
        }
    }

    public boolean isPassed() {
        long diff = timeDiff();
        return (diff >= 0) && (diff < 180);
    }
    public String toString(){
        return stunde + ":" + minute + ":00";
    }
    public boolean isInRange() {
        long diff = timeDiff();
        if (diff < 0) {
            diff = 0 - diff;
        }
        return diff < UselessWecker.COFFEE_RANGE;
    }
}
